<?php

namespace Keio\StudyBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Keio\StudyBundle\Entity\Blog;
use Keio\StudyBundle\Form\BlogType;

/**
 * Blog controller.
 *
 */
class BlogController extends Controller
{

    /**
     * Lists all Blog entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        //lấy resume max là 500 ký tự thôi
        $entities = $em->getRepository('KeioStudyBundle:Blog')->getBlogResume(500);

        return $this->render('KeioStudyBundle:Blog:index.html.twig', array(
            'entities' => $entities,
        ));
    }
    public function listAction()
    {
        $em = $this->getDoctrine()->getManager();

        //chỉ lấy 50 ký tự cho gọn bảng
        $entities = $em->getRepository('KeioStudyBundle:Blog')->getBlogResume(150);
        //set delete form cho mỗi element
        foreach ($entities as $entity){
        	$deleteForm = $this->createDeleteForm($entity->getId());
        	$entity->setDelform($deleteForm->createView());
        }

        return $this->render('KeioStudyBundle:Blog:bloglist.html.twig', array(
            'entities' => $entities,
        ));
    }
    /**
     * Creates a new Blog entity.
     *
     */
    public function createAction(Request $request)
    {
    	$user_id  = $this->get('security.context')->getToken()->getUser()->getId();
    	$em = $this->getDoctrine()->getManager();

        $user = $em->getRepository('KeioStudyBundle:User')->find($user_id);

        $request = $this->getRequest();
		$data_form = $request->request->get('keio_studybundle_blogtype');
    	$dat_title = $data_form['title'];
    	$dat_cat = $em->getRepository('KeioStudyBundle:Blogcategory')->find($data_form['category']);
    	$dat_blog = $data_form['blog'];


        $blog  = new Blog();
        $blog->setUser($user);
        $date = new \DateTime();
        $blog->setDatetime($date);
        $blog->setDatemodif($date);
        $blog->setTitle($dat_title);
        $blog->setBlog($dat_blog);
        $blog->setCategory($dat_cat);

        $em->persist($blog);
        $em->flush();

            return $this->redirect($this->generateUrl('blog_list'));
    }

    /**
     * Displays a form to create a new Blog entity.
     *
     */
    public function newAction()
    {
        $entity = new Blog();
        $form   = $this->createForm(new BlogType(), $entity);

        return $this->render('KeioStudyBundle:Blog:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Blog entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Blog')->find($id);
        $cmt    = $em->getRepository('KeioStudyBundle:Blogcomment')->getCommentByBlog($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Blog entity.');
        }

        return $this->render('KeioStudyBundle:Blog:show.html.twig', array(
            'entity'      => $entity,
            'comment'     => $cmt,
        ));
    }

    /**
     * Displays a form to edit an existing Blog entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Blog')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Blog entity.');
        }

        $editForm = $this->createForm(new BlogType(), $entity);

        return $this->render('KeioStudyBundle:Blog:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'noidungblog' => $entity->getBlog(),
        ));
    }

    /**
     * Edits an existing Blog entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $blog = $em->getRepository('KeioStudyBundle:Blog')->find($id);


        $request = $this->getRequest();
		$data_form = $request->request->get('keio_studybundle_blogtype');
    	$dat_title = $data_form['title'];
    	$dat_cat = $em->getRepository('KeioStudyBundle:Blogcategory')->find($data_form['category']);
    	$dat_blog = $data_form['blog'];


        $date = new \DateTime();
        $blog->setDatemodif($date);
        $blog->setTitle($dat_title);
        $blog->setBlog($dat_blog);
        $blog->setCategory($dat_cat);

        $em->persist($blog);
        $em->flush();

            return $this->redirect($this->generateUrl('blog'));


    }
    /**
     * Deletes a Blog entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('KeioStudyBundle:Blog')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Blog entity.');
            }

            $cmt    = $em->getRepository('KeioStudyBundle:Blogcomment')->getCommentByBlog($id);
            //delete comment
            foreach ($cmt as $c){
            	$em->remove($c);
            }
            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('blog_list'));
    }

    /**
     * Creates a form to delete a Blog entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
